﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace P.InOut.NetMvc.Common
{
    public static class Utilities
    {
        public static string[] Sexes = { "Male", "Female" };

        public static string GetTimeElapsed(DateTime dateTime)
        {
            int hours = (int)Math.Abs(dateTime.Subtract(DateTime.Now).TotalHours);
            string result = string.Empty;

            if (hours < 24)
                if (hours < 1)
                    result = "just recently";
                else
                    result = string.Format("around {0} hours ago", hours.ToString());
            else
                result = string.Format("around {0} days ago", (hours / 24).ToString());

            return result;
        }

        public static string GetFormattedDate(DateTime datetime)
        {
            string day = Enum.GetName(typeof(DayOfWeek), datetime.DayOfWeek);
            string month = Enum.GetName(typeof(MonthOfYear), datetime.Month);

            return string.Format("{0}, {1} {2}", day, month, datetime.Day);
        }

        public static string FormatText(string text)
        {
            // HtmlEncode text
            text = System.Web.HttpUtility.HtmlEncode(text);

            /*
             *  Specifies single-line mode. Changes the meaning of the period
                character (.) so that it matches every character (instead of every
                character except \n).
             */


            // replace breaks with newlines so this way \b can be used
            text = Regex.Replace(text, @"(<br>|<br />)", "\n", RegexOptions.IgnoreCase);

            // these help with the url to link replacement
            text = Regex.Replace(text, @"&quot;", @"""");
            text = Regex.Replace(text, @"&gt;", ">");
            text = Regex.Replace(text, @"&lt;", "<");

            // replace all recognizable urls with clickable links (check to make sure the url starts with http)
            text = Regex.Replace(text, @"\b(https?://)?[-a-z0-9]+(\.[-a-z0-9]+)*\.(com|net|org|gov|edu|biz|int|info|mil|name|museum|coop|aero|[a-z][a-z])\b(:\d+)?([^<>""\s')]*)?(?<![.,!;?])",
                "\uffe9a href=\"$&\" title=\"$&\"\uffeb$&\uffe9/a\uffeb", RegexOptions.IgnoreCase | RegexOptions.Singleline);

            // url encode &gt;
            text = Regex.Replace(text, ">", "&gt;");        // re-encode html
            text = Regex.Replace(text, "<", "&lt;");        // re-encode html
            text = Regex.Replace(text, "\uffe9", "<");      // replace unicode chars
            text = Regex.Replace(text, "\uffeb", ">");      // replace unicode chars

            // replace any links with issues...like no href="http://
            text = Regex.Replace(text, @"<a\s+href=""(?!http://)", @"<a href=""http://", RegexOptions.IgnoreCase | RegexOptions.Singleline);

            // replace only link &gt; to >
            //text = Regex.Replace(text, @"</a&gt;", "</a>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            //text = Regex.Replace(text, @"(\s+title="".*?"")(&gt;)", "$1>", RegexOptions.IgnoreCase | RegexOptions.Singleline);

            // return quotes back to &quot;
            text = Regex.Replace(text, @"""", "&quot;");
            // replace only link &quot; to "
            text = Regex.Replace(text, @"(<a\s+href=)(?<q1>&quot;)(.*?)(?<q2>&quot;)(\s+title=)(?<q3>&quot;)(.*?)(?<q3>&quot;)", @"$1""$2""$3""$4""",
                RegexOptions.IgnoreCase | RegexOptions.Singleline);
            // return text with \n returned back to html breaks
            text = Regex.Replace(text, "\n", "<br />", RegexOptions.IgnoreCase);

            //emoticons
            text = Regex.Replace(text, @"(\s+|^)(:|;|=)(-)?(\(|\)|D|P|p|o)", "<span class=\"emoticon\">$&</span>");

            return text;
        }
    }
}
